package com.codeplex.tdmg4mc;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.client.IItemRenderer;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import com.google.common.collect.Multimap;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemDualSword extends ItemSword implements IItemRenderer
{
	private IIcon inventoryIcon;
	private IIcon inventoryIconEmpty;
	private IIcon renderIcon;
	private IIcon renderIconEmpty;
	private boolean refresh=false;
	private boolean breakSound=false;
	private ItemStack itemStack;
	public ItemDualSword(ToolMaterial par2EnumToolMaterial)
	{
		super(par2EnumToolMaterial);
		setMaxDamage(50);
		setNoRepair();
		setCreativeTab(CreativeTabs.tabCombat);
	}
	
    public Multimap getItemAttributeModifiers()
    {
        Multimap multimap = super.getItemAttributeModifiers();
        if(this.itemStack!=null&&this.itemStack.getItemDamage()>=this.getMaxDamage())
        {
        	multimap.removeAll(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName());
        	multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Weapon modifier", 0, 0));
        }
        return multimap;
    }
	
	@Override
	public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, Block par3Block, int par4, int par5, int par6, EntityLivingBase par7EntityLiving)
	{
		itemStack=par1ItemStack;
		if(par3Block.getBlockHardness(par2World, par4, par5, par6)!=0.0f)
		{
			par1ItemStack.attemptDamageItem(2, itemRand);
			if(par1ItemStack.getItemDamage()==par1ItemStack.getMaxDamage()||par1ItemStack.getItemDamage()==par1ItemStack.getMaxDamage()+1) breakSound=true;
		}
		return true;
	}
	
	@Override
	public boolean hitEntity(ItemStack par1ItemStack, EntityLivingBase par2EntityLiving, EntityLivingBase par3EntityLiving)
	{
		itemStack=par1ItemStack;
		par1ItemStack.attemptDamageItem(1, itemRand);
		if(par1ItemStack.getItemDamage()==par1ItemStack.getMaxDamage()) breakSound=true;
		return true;
	}
	
	@Override
	public void onUpdate(ItemStack item, World world, Entity entity, int par4, boolean par5)
	{
		itemStack=item;
		if(breakSound)
		{
			world.playSound(entity.posX, entity.posY, entity.posZ, "mob.blaze.hit", 1f, 1f, false);
			breakSound=false;
		}
		if(refresh)
		{
			item.setItemDamage(0);
			world.playSound(entity.posX, entity.posY, entity.posZ, "mob.sheep.shear", 1f, 1f, false);
			refresh=false;
		}
	}
	
	@Override
	public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player)
	{
		itemStack=item;
		if(item.getItemDamage()>=item.getMaxDamage()&&player.inventory.consumeInventoryItem(TDMG.blade)) refresh=true;
		return item;
	}

	public IIcon getIconFromDamage(int damage)
	{
		if(damage>=getMaxDamage()) return inventoryIconEmpty;
		else return inventoryIcon;
	}
	
	@SideOnly(Side.CLIENT)
	@Override
	public void registerIcons(IIconRegister ir)
	{
		super.registerIcons(ir);
		inventoryIcon=ir.registerIcon("tdmg:dualsword");
		inventoryIconEmpty=ir.registerIcon("tdmg:dualswordempty");
		renderIcon=ir.registerIcon("tdmg:sword");
		renderIconEmpty=ir.registerIcon("tdmg:swordempty");
	}

	@Override
	public boolean handleRenderType(ItemStack item, ItemRenderType type)
	{
		itemStack=item;
		return type==ItemRenderType.EQUIPPED||type==ItemRenderType.EQUIPPED_FIRST_PERSON;
	}

	@Override
	public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper)
	{
		itemStack=item;
		return false;
	}

	@Override
	public void renderItem(ItemRenderType type, ItemStack item, Object... data)
	{
		itemStack=item;
        GL11.glPushMatrix();
        GL11.glEnable(GL12.GL_RESCALE_NORMAL);
        Tessellator ts=Tessellator.instance;
        IIcon icon=item.getItemDamage()<item.getMaxDamage()?renderIcon:renderIconEmpty;
        ItemRenderer.renderItemIn2D(ts, icon.getMaxU(), icon.getMinV(), icon.getMinU(), icon.getMaxV(), icon.getIconWidth(), icon.getIconHeight(), 0.0625F);
        GL11.glDisable(GL12.GL_RESCALE_NORMAL);
        GL11.glPopMatrix();
	}
}
